package cn.jly.springboot24.controller;

import cn.jly.springboot24.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName cn.jly.springboot24.controller
 * @ClassName RequestParamsControlelr
 * @Description 测试获取请求参数
 * @Author 姬岚洋
 * @Date 2021/1/17 下午 5:11
 */
@Controller
public class RequestParamsControllr {

    /**
     * 各种请求参数列子
     *
     * @param id
     * @param username
     * @param pathVariablesMap
     * @param host
     * @param headerParamsMap
     * @param age
     * @param requestParamsMap
     * @param _ga
     * @param cookie
     * @return
     */
    @ResponseBody
    @GetMapping("/test/{id}/owner/{username}")
    public Map<String, Object> testParams(
            @PathVariable("id") Integer id,
            @PathVariable("username") String username,
            @PathVariable Map<String, String> pathVariablesMap,
            @RequestHeader("Host") String host,
            @RequestHeader Map<String, String> headerParamsMap,
            @RequestParam("age") Integer age,
            @RequestParam Map<String, Object> requestParamsMap,
            @CookieValue(value = "_ga", required = false) String _ga,
            @CookieValue(value = "_ga", required = false) Cookie cookie
    ) {
        final HashMap<String, Object> map = new HashMap<>();
        // pathVariable
        map.put("id", id);
        map.put("username", username);
        map.put("pathVariablesMap", pathVariablesMap);
        // requestHeader
        map.put("host", host);
        map.put("headerParamsMap", headerParamsMap);
        // requestParam
        map.put("age", age);
        map.put("requestParamsMap", requestParamsMap);
        // cookie
        map.put("_ga", _ga);
        map.put("cookie", cookie);

        return map;
    }

    /**
     * 请求体例子
     *
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/testRequestBody")
    public Map<String, Object> testRequestBody(@RequestBody User user) {
        final HashMap<String, Object> map = new HashMap<>();
        map.put("user", user);
        return map;
    }

    /**
     * 请求转发例子
     *
     * @param request
     * @return
     */
    @GetMapping("/goto")
    public String forwardRequest(HttpServletRequest request) {
        request.setAttribute("msg", "成功啦");
        return "forward:/success";
    }

    @ResponseBody
    @GetMapping("/success")
    public Map<String, Object> success(@RequestAttribute("msg") String msg) {
        final HashMap<String, Object> map = new HashMap<>();
        map.put("msg", msg);
        return map;
    }

    /**
     * 矩阵变量例子
     * <p>
     * spring boot默认禁用了矩阵变量的功能，需要手动去开启
     * 原理：对于整个路径的处理，都是使用UrlPathHelper进行解析，removeSemicolonContent = true，该属性设置是移除分号后面的内容
     * 解决：WebMvcConfig#configurePathMatch#urlPathHelper.setRemoveSemicolonContent(false);
     * <p>
     * 正常请求：/testMatrixVariables/{pathVariable}?k1=v1&k2=v2，这个叫查询字符串queryString，使用@RequestParam取出
     * 矩阵变量：/testMatrixVariables/sell;low=34;brand=byd,audi,yd，矩阵变量，使用分号分隔；多用在cookie被禁用是，使用此方式对url进行重写，携带cookie信息，比如jsessionId
     * /boss/1;age=23/2;age=44，这种写法也可以
     * <p>
     * 矩阵变量必须伴随着url路径变量才能被解析
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/testMatrixVariables/{path}")
    public Map<String, Object> testMatrixVariables(
            @MatrixVariable("low") Integer low,
            @MatrixVariable("brand") List<String> brand,
            @PathVariable("path") String path
    ) {
        final HashMap<String, Object> map = new HashMap<>();
        map.put("low", low);
        map.put("brand", brand);
        map.put("path", path);

        /*
        示例请求：localhost:8888/testMatrixVariables/sell;low=34;brand=byd,audi,yd
        返回：
        {
            "path": "sell",
            "low": 34,
            "brand": [
                "byd",
                "audi",
                "yd"
            ]
        }
         */
        return map;
    }

    /**
     * /boss/1;age=23/2;age=44
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/boss/{bossId}/{empId}")
    public Map<String, Object> testMatrixVariables2(
            @MatrixVariable(pathVar = "bossId", value = "age") Integer bossAge,
            @MatrixVariable(pathVar = "empId", value = "age") Integer empAge
    ) {
        final HashMap<String, Object> map = new HashMap<>();
        map.put("bossAge", bossAge);
        map.put("empAge", empAge);
        return map;
    }
}
